<?php
/**
 * Created by PhpStorm
 * User: lcz
 * Date: 2019/10/14
 * Time: 15:40
 * 团队
 */
namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\UserLayer;
use think\Db;

class Team extends Api {
	
	/**
	 * 统计
	 */
	public function total() {
		$model = new UserLayer();
		$userId = $this->auth->id;
		$indirect = $model->where([
			'parent_id' => $userId,
			'layer' => 2
		])->count();
		
		$direct = $model->where([
			'parent_id' => $userId,
			'layer' => 1
		])->count();
		
		$all = $model->where(['parent_id' => $userId])->count();
		
		$this->success('查询成功', [
			'indirect'	 => $indirect,
			'direct' => $direct,
			'all' => $all
		]);
		
	}
	
	
	/**
	 * 下级
	 */
	public function index() {
		$model = new UserLayer();
		$userId = $this->auth->id;
		
		$where['l.parent_id'] = $userId;
		
		$layer = $this->request->post('layer');
		$page = $this->request->post('page', 1, 'intval');
		
		$layer && $where['l.layer'] = $layer;
		$all = $model->alias('l')->where($where)->count();
		
		$list = Db::name('userLayer')->alias('l')
			->join('user u', 'l.user_id = u.id', 'left')
			->where($where)
			->page($page)
			->field('u.id, u.mobile, u.nickname, from_unixtime(jointime, \'%Y-%m-%s\') as date')
			->select();
		
		if($list) {
			foreach ($list as &$item) {
				$item['mobile'] = substr($item['mobile'], 0, 3).'****'.substr($item['mobile'],7, 4);
			}
		}
		
		$this->success('查询成功', [
			'list'	 => $list,
			'page' => $page,
			'total' => $all
		]);
	}
}
